<?php

namespace App\Http\Controllers\Course;

use App\Models\Article;
use App\Models\EnrollTeacher;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\Tags;
use App\Models\Ctype;
use App\Models\CourseClass;
use App\Models\CourseSection;
use App\Models\CourseTag;
use App\Models\Studying;
use App\Models\Collect;
use App\Models\PushLog;
use App\Models\UserAttribute;
use App\User;
use App\Contant\TagsType;
use App\Utils\FileUploader;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\Banner;
use App\Models\Consultation;
use Illuminate\Support\Facades\Input;
use App\Events\WxMessagePush;
use App\Contant\WxMessageType;
use App\Events\WxCustomerMessagePush;
use App\Models\Special;

class CourseController extends Controller
{
    protected $sectionsArr = ['one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5,'six'=>6,'seven'=>7,'eight'=>8,'nine'=>9,'ten'=>10,'eleven'=>11,'twelve'=>12,'thirteen'=>13,'fourteen'=>14,'fifteen'=>15,'sixteen'=>16,'seventeen'=>17,'eighteen'=>18,'nineteen'=>19,'twenty'=>20,'twentyOne'=>21,'twentyTwo'=>22,'twentyThree'=>23,'twentyFour'=>24,'twentyFive'=>25,'twentySix'=>26,'twentySeven'=>27,'twentyEight'=>28,'twentyNine'=>29,'thirty'=>30,'thirtyOne'=>31,'thirtyTwo'=>32,'thirtyThree'=>33,'thirtyFour'=>34,'thirtyFive'=>35,'thirtySix'=>36,'thirtySeven'=>37,'thirtyEight'=>38,'thirtyNine'=>39,'forty'=>40];
    protected $sectionPinyin = ['第一节','第二节','第三节','第四节','第五节','第六节','第七节','第八节','第九节','第十节','第十一节','第十二节','第十三节','第十四节','第十五节','第十六节','第十七节','第十八节','第十九节','第二十节','第二十一节','第二十二节','第二十三节','第二十四节','第二十五节','第二十六节','第二十七节','第二十八节','第二十九节','第三十节','第三十一节','第三十二节','第三十三节','第三十四节','第三十五节','第三十六节','第三十七节','第三十八节','第三十九节','第四十节'];
    protected $sectionNumber = ['one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty','twentyOne','twentyTwo','twentyThree','twentyFour','twentyFive','twentySix','twentySeven','twentyEight','twentyNine','thirty','thirtyOne','thirtyTwo','thirtyThree','thirtyFour','thirtyFive','thirtySix','thirtySeven','thirtyEight','thirtyNine','forty'];
    public function __construct()
    {

    }

    public function index(Request $request){
        $title = $request->input('title','');
        $courseClass = new CourseClass();
        $liveClass = new CourseClass();
        if($title){
            $courseClass = $courseClass->where('title','like',"%{$title}%");
        }

        if($request->input('is_live')){
            $courseClass = $courseClass->where('is_live',1);
        }else{
            $courseClass = $courseClass->where('is_live',0);
        }

        $courseClass = $courseClass->orderBy("orderby","desc")->orderBy('id','desc')->paginate(10);
        $data['courseClass'] = $courseClass;
        $data['title'] = $title;
        $data['is_live'] = $request->input('is_live','');
        return view('course.index',$data);
    }

    public function create(Request $request){
        $tags = Tags::where('state',1)->select('id','title')->get();
       // $courseTypes = Ctype::where('state',1)->select('id','title')->get();
        $cType = new Ctype();
        $type = $cType->gType();
        $courseTypes = $cType->getTree($type,0);
        $data['tags'] = $tags;
        $data['courseTypes'] = $courseTypes;
        return view('course.create',$data);
    }

    public function postCreate(Request $request){
        DB::beginTransaction();
        try{
            $this->dealCourseData($request);
            DB::commit();
            Redis::del('select_course_class');
            $this->getSessionInfo($request,'success');
            return redirect('/course/list');
        }catch(\Exception $e){
            DB::rollback();
            $this->getSessionInfo($request,'error');
            return false;
        }
    }

    public function edit(Request $request){

        $class_id = $request->input('id');
        $tags = Tags::where('state',1)->select('id','title')->get();
        //$courseTypes = Ctype::where('state',1)->select('id','title')->get();
        $cType = new Ctype();
        $type = $cType->gType();
        $courseTypes = $cType->getTree($type,0);
        $courseClass = CourseClass::where('id',$class_id)->first();
        $courseSections = CourseSection::where('course_class_id',$class_id)->get();
        $courseTag = CourseTag::where('course_class_id',$class_id)->select('id','tag_id')->get();
        $courseTags = [];
        foreach($courseTag as $tag){
            $courseTags[] = $tag->tag_id;
        }
        $cType = new Ctype();
        $type = $cType->gType();
        $data['sectionPinyin'] = $this->sectionPinyin;
        $data['sectionNumber'] = $this->sectionNumber;
        $data['courseTags'] = $courseTags;
        $data['tags'] = $tags;
        $data['courseTypes'] = $courseTypes;//$courseTypes
        //dd($courseTypes);
        $data['courseSections'] = $courseSections;
        $data['courseClass'] = $courseClass;
        return view('course.edit',$data);
    }

    public function postEdit(Request $request){
        DB::beginTransaction();
        try{
            $this->dealCourseData($request);
            DB::commit();
            Redis::del('select_course_class');
            $this->getSessionInfo($request,'success');
            return redirect('/course/list');
        }catch(\Exception $e){
            DB::rollback();
            logger()->info($e->getMessage());
            $this->getSessionInfo($request,'error');
            return false;
        }
    }

    /*
     * 课程详情
     */
    public function detail(Request $request){
        $class_id = $request->input('id');
        $courseClass = CourseClass::where('id',$class_id)->first();
        $data['courseClass'] = $courseClass;
        $data['sectionPinyin'] = $this->sectionPinyin;
        return view('course.detail',$data);
    }

    /*
     *设置是否预览
     */
    public function postView(Request $request){
        $ids = $request->input('id');
        $preview = $request->input('preview');
        $idArr = explode('_',$ids);
        $id = $idArr[1];
        $class_id = $idArr[0];
//        $preClass = Course::where('course_class_id',$class_id)->where('preview',1)->where('id','!=',$id)->first();
        $code = 0;
        $message = '设置成功';
        $flag = 0;
        $course = Course::where('id',$id)->first();
        if($course){
            $course->preview = $preview;
            if($course->save()){
                $flag = 1;
            }else{
                $message = '设置失败';
            }
        }else{
            $message = '设置失败';
        }

        if($flag == 0){
            $code = 1;
        }
        return $this->getMessage($code,$message);
    }

    /*
     * 修改课程状态
     */
    public function changeState(Request $request){
        $id = $request->input('id');
        $state = $request->input('state');
        $course = Course::where('id',$id)->first();
        $course->state  = $state;
        if($course->save()){
            return $this->getMessage(0,'修改成功');
        }else{
            return $this->getMessage(1,'修改状态失败');
        }
    }
    /*
     * 修改课程分类状态
     */
    public function changeClassState(Request $request){
        $id = $request->input('id');
        $state = $request->input('state');
        $courseClass = CourseClass::where('id',$id)->first();
        $courseClass->state = $state;
        if($state){
            $name = '上架';
        }else{
            $name = '下架';
        }
        if($courseClass->save()){
            return $this->getMessage(0,$name.'成功');
        }else{
            return $this->getMessage(1,$name.'失败');
        }
    }
    /*
     * 删除课程
     */
    public function delete(Request $request){
        $id = $request->input('id');
        $result = Course::where('id',$id)->delete();

        if($result){
            return $this->getMessage(0,'删除成功');
        }else{
            return $this->getMessage(1,'删除失败');
        }
    }
    /*
     * 删除课程（整体）
     */
    public function classDelete(Request $request){
        $id = $request->input('id');
        $result = CourseClass::where('id',$id)->delete();

        if($result){
            return $this->getMessage(0,'删除成功');
        }else{
            return $this->getMessage(1,'删除失败');
        }
    }
    /*
     * 处理表单信息
     */
    private function dealCourseData(Request $request){
        //课程栏目
        $class_id = $request->input('class_id','');
        if($class_id){
            $courseClass = CourseClass::where('id',$class_id)->first();
        }else{
            $courseClass = new CourseClass();
        }
        //dd($request->input('is_video'));
        $courseClass->seo_title = $request->input('seo_title');
        $courseClass->seo_description = $request->input('seo_description');
        $courseClass->title = $request->input('name');
        $courseClass->cover_url = $request->input('coverImg');
        $courseClass->explain_url = $request->input('explainImg');
        $courseClass->course_type_id = $request->input('courseType');
        $courseClass->user_id = $request->input('author');
        $courseClass->orderby = $request->input("orderby");
        $courseClass->studying_num = $request->input("studying_num");
        $courseClass->introduction = $request->input('introduction');
        $courseClass->push_message = $request->input('pushMessage');
        $courseClass->opertion_user_id = $request->user()->id;
        $courseClass->price = $request->input('price',0);
        $courseClass->preview = $request->input('preview',1);
        $courseClass->level = $request->input('level');
        $courseClass->is_hot = $request->input('is_hot');
        $courseClass->is_hide = $request->input('is_hide', 0);
//        $courseClass->is_live = $request->input('is_live',0);

        if(!$courseClass->price){
            $courseClass->price = 0;
        }
        $courseClass->is_free = $request->input('payFee',0);
        $courseClass->save();
        //栏目标签
        $this->dealClassTag($request,$courseClass->id);

        //课程小节管理
        foreach($this->sectionsArr as $key => $section){
            $course_section = $request->input('course_section_'.$key);
            $course_section_id = $request->input('course_section_id_'.$key,'');
            $course_name = $request->input('course_name_'.$key);
            $course_ids = $request->input('course_id_'.$key);
            $course_address = $request->input('course_address_'.$key);
            if($course_section){
                //添加小节
                if($course_section_id){
                    $courseSection = CourseSection::where('id',$course_section_id)->first();
                }else{
                    $courseSection = new CourseSection();
                }
                $courseSection->title = $request->input('course_section_'.$key);
                $courseSection->course_class_id = $courseClass->id;
                $courseSection->opertion_user_id = $request->user()->id;
                $courseSection->num = $section;
                $courseSection->save();
//                dd(3);
                if($course_name){
                    foreach($course_name as $courseKey => $courseName){
                        //添加课程
                        if(isset($course_ids[$courseKey])){
                            $course = Course::where('id',$course_ids[$courseKey])->first();
                        }else{
                            $course = new Course();
//                            if($key == 0 && $courseKey == 0 && $courseClass->is_free == 1){
//                                $course->preview = 1;
//                            }
                        }
                        $course->title = $courseName;
                        $course->course_class_id = $courseClass->id;
                        $course->course_class_section_id = $courseSection->id;
                        $course->opertion_user_id = $request->user()->id;
                        $course->video_url = $course_address[$courseKey];
                        $course->save();
                    }
                }

            }else{
                break;
            }
        }
        return true;

    }
    /*
     * 选择作者
     */
    public function selectAuthor(Request $request){
        $users = new User();
        $name = $request->input('name','');
        if($name){
            $users  = $users->where('name','like',"%$name%");
        }
        $users = $users->where('is_author',1)->paginate(5);
        foreach($users as &$user){
            $remark = UserAttribute::where("user_id", $user->id)->first();
            $user->remark = empty($remark) ? "---" : $remark->remark;
        }

        return json_encode(['code'=>0,'body'=>view('course.modal.body',['users'=>$users,'name'=>$name])->render()]);
    }
    /*
     *标签处理
     */
    private function dealClassTag(Request$request,$class_id){
        $courseTags = CourseTag::where('course_class_id',$class_id)->get();
        $tagsArr = [];
        foreach($courseTags as $courseTag){
            $tagsArr[] = $courseTag->tag_id;
        }
        $tags = $request->input('tags');
        foreach($tags as $tag){
            //课程标签关联表
            if(!in_array($tag ,$tagsArr)){
                $courseTag = new CourseTag();
                $courseTag->course_class_id = $class_id;
                $courseTag->tag_id = $tag;
                $courseTag->save();
            }
        }
        $diffTag = array_diff($tagsArr,$tags);
        if($diffTag){
            CourseTag::where('course_class_id',$class_id)->whereIn('tag_id',$diffTag)->delete();
        }
    }
    /*
     * 图片上传
     */
    public function postUpload(Request $request){
        $file = new FileUploader($request);
        $fileInfo = $file->base64ImgUpload($request);
        return $fileInfo;
    }

    public function postUploadAuto(Request $request){
        $file = new FileUploader($request);
        logger()->info(env('ROOT_DIR'));
        $fileInfo = $file->base64ImgUpload($request,'', env('ROOT_DIR'));
        return $fileInfo;
    }
    /*
     * 课程在线咨询20180827
     */
    public function consultation(Request $request){
        $consultation   = new Consultation();
        $wherearr = [];
        $where    = [];
        if (Input::has('keyword')){
            $keyword = Input::get('keyword');
            if(is_numeric($keyword)){
                $where['id'] = $keyword;
            }else{
                $wherearr['targets'] = $keyword;
            }
        }
        if (Input::has('state')){
            $where['state'] = Input::get('state');
        }

        $list = $consultation->listinfo($where, $wherearr);
        
        $this->ret['list'] = $list;
        $this->ret['state']= Input::get('state');
        $this->ret['keyword']= Input::get('keyword');
        return view('course.consultation', $this->ret);
    }

    public function pushMessage(Request $request){
        $class_id = $request->input('id');
        $courseClass = CourseClass::where('id',$class_id)->select('id','title','user_id')->first();
        $author  = $courseClass->author;

        $fans = $author->fans($author->id);


        $data['type'] = WxMessageType::IMAGE;
        $author = $courseClass->author;

        $title = "【新课上线】".$courseClass->title;
        $desc = $author->name."[导师系列课]：\n".$courseClass->push_message."\n点击进入即可查看系列课内容\n回复[TD]".$courseClass->id."不再接收该导师系列课推送";
        $data['list'] = [[
            "title"=>$title,
            "description"=>$desc,
            "url"=>env('PT_WEB_URL').'/course/detail?id='.$data['course_class_id'],
            "picurl"=>'http://image.saipubbs.com/'.$courseClass->explain_url]];
        if($courseClass->push_message){
            foreach($fans as $fan){
                $data['openid'] = $fan->openid;
                event(new WxCustomerMessagePush($data));
            }
        }
//        $data['type'] = WxMessageType::CREATE;
//        $data['url'] = env('APP_URL').'/course/detail?id='.$class_id;
//        $data['notice'] = '发布新课程啦';
//        $data['message']['course'] = $courseClass;
//        $data['message']['author'] = $author;
//
//        foreach($fans as $fan){
//            $data['openid'] = $fan->openid;
//            $data['user'] = $fan;
//            event(new WxMessagePush($data));
//        }
        return $this->getMessage(0,'推送成功',[]);
    }
    /*
     * 课程在线咨询20180828
     */
    public function consultationState(Request $request){
        $id    = $request->input('id');
        $state = $request->input('state'); 
        if($id && $state){
            $consultation = Consultation::where('id',$id)->first();
            $consultation->state= $state;
            $r  = $consultation->save();
            if($r){
                return json_encode(array("code"=>1, "msg"=>"确认成功"));
            }
        }
         
        return json_encode(array("code"=>0, "msg"=>"确认失败稍后请重试"));;
    }

    /**
     * 新课速递
     */
    public function is_new(Request $request){
        $data = $request->input();
        if($data['data'] == 0){
            $num['is_new'] = 1;
        }else{
            $num['is_new'] = 0;
        }
         $course = new CourseClass();
         $id = $data['id'];
         $re = $course->where("id",$id)->update($num);
         
         if($re){
            return redirect("course/list");
         }
    }

    /*
     *用户收藏统计
     */
    public function collectCount(Request $request,$id){
        $collect = Collect::where('course_class_id',$id)->paginate(10);
//        dd($collect);
        return view('course.collect',['collects'=>$collect]);
    }

    /*
     * 免费观看
     */
    public function freeWatch(Request $request){
        $id = $request->input('id');
        $courseClass = CourseClass::where('id',$id)->first();
        $courseClass->register_free_watch = $request->input('free_watch');
        if($courseClass->save()){
            return $this->getMessage(0,'设置成功');
        }else{
            return $this->getMessage(1,'设置失败');
        }
    }
    public function updateorder(){
        $list = DB::table("order")->where("state",0)->select("id", "user_id","course_class_id")->get();
        $study = new Studying();
        foreach($list as $v){
            $study->addOne($v->user_id, $v->course_class_id);
        }
        dd($list);
    }
    public function allMessagePush(Request $request,$id){

        $data['courseClass'] = CourseClass::where('id',$id)->first();
        $data['pushType'] = [WxMessageType::CREATE=>'课程发布',WxMessageType::UPDATE=>'课程更新',WxMessageType::COLLECT=>'课程收藏',WxMessageType::RECOMMEND=>'好课推荐',WxMessageType::WEIXINNOTICE=>'进群通知',WxMessageType::COURSEBUY=>'购买课程'];
        $data['pushLogs'] = PushLog::where('course_class_id',$id)->select('id','type','description','created_at')->orderBy('id','desc')->get();
        return view('course.coursePush',$data);
    }

    public function dealMessagePush(Request $request){
        $type = $request->input('type');
        $class_id = $request->input('id');
        $startTime = $request->input('startTime','');
        $endTime = $request->input('endTime','');
        $courseId = $request->input('courseId','');
        $courseClass = CourseClass::where('id',$class_id)->first();

        $author = $courseClass->author;
        $info = $this->getInfo($type,$author);
        if($type == WxMessageType::WEIXINNOTICE){
            $title = "孕产教练交流群上线了";
        }elseif($type == WxMessageType::COURSEBUY){
            $title = "【新课推荐】".$courseClass->title;
        }else{
            $title = "【".WxMessageType::trans($type)."】".$courseClass->title;
        }

        $data['type'] = WxMessageType::IMAGE;
        $push_message = $courseClass->push_message.
        "\n";
        if($type != WxMessageType::CREATE){
            $push_message = '';
        }
        if($info !=''){
            $info = $info."\n";
        }
        if($type == WxMessageType::WEIXINNOTICE){
            $desc = '加微信号：SPJSSQ，加入孕产教练交流群与田坤导师互动交流~';
            $url = env('PT_WEB_URL').'/course/detail/42.html';
        }else {
            $desc = $author->name . "[导师系列课]：\n" . $info . $push_message . "点击进入即可查看系列课内容\n回复[TD" . $courseClass->id . "]不再接收该导师系列课推送";
            $url = env('PT_WEB_URL').'/course/detail/'.$class_id.'.html';
        }
        $data['list'] = [[
            "title"=>$title,
            "description"=>$desc,
            "url"=>$url,
            "picurl"=>'http://image.saipubbs.com/'.$courseClass->explain_url]];

        $fans = $this->getPushUsers($type,$courseClass,$author,$startTime,$endTime,$courseId);

        foreach($fans as $fan){
            $data['openid'] = $fan->openid;
//            dd($fan);
            event(new WxCustomerMessagePush($data));
        }
        $pushLog = new PushLog();
        $pushLog->course_class_id = $courseClass->id;
        $pushLog->type = $type;
        $pushLog->description = WxMessageType::trans($type);
        $pushLog->start = $startTime;
        $pushLog->end = $endTime;
        $pushLog->save();

        return $this->getMessage(0,'推送成功');
    }

    /*
     * 自定义消息推送
     */
    public function defineMessasgePush(Request $request){

        $class_id = $request->input('id');
        $content = $request->input('content');
        $title = $request->input('title');
        $courseClass = CourseClass::where('id',$class_id)->first();

        $author = $courseClass->author;
        $info = $this->getInfo(WxMessageType::UPDATE,$author);

        $data['type'] = WxMessageType::IMAGE;
        $contentArr = explode(PHP_EOL,$content);
        $desc = '';
        foreach($contentArr as $cont){
            $desc .= $cont."\n";
        }
//        dd($desc);
        $data['list'] = [[
            "title"=>$title,
            "description"=>$desc,
            "url"=>env('PT_WEB_URL').'/course/detail/'.$class_id.'.html',
            "picurl"=>'http://image.saipubbs.com/'.$courseClass->explain_url]];

        $fans = $this->getPushUsers(WxMessageType::UPDATE,$courseClass,$author);

        foreach($fans as $fan){
            $data['openid'] = $fan->openid;
//            $data['openid'] = $user->openid;
//            dd($fan);
            event(new WxCustomerMessagePush($data));
        }
        $pushLog = new PushLog();
        $pushLog->course_class_id = $courseClass->id;
        $pushLog->type = 'DEFINE';
        $pushLog->description = WxMessageType::trans('DEFINE').'|'.$desc;
        $pushLog->start = '';
        $pushLog->end = '';
        $pushLog->save();

        return $this->getMessage(0,'推送成功');
    }
    /*
     * 导师消息推送
     */
    public function messagePush(Request $request){
        $type = $request->input('type');
        $article_id = $request->input('aritcle_id','');
        $pushObj = $request->input('pushObj','');
        $courseId = $request->input('courseId','');
        if($type == 1){
            $article = Article::where('id',$article_id)->select('id','title','special_id','cover_url')->first();
            $title = $article->title;
            $url = env('PT_WEB_URL')."/article/special/".$article->id."/".$article->special_id.".html";
            $picUrl = env('IMG_URL').$article->cover_url;
        }else{
            $course = CourseClass::where('id',$courseId)->first();
            $title = $course->title;
            $url = env('PT_WEB_URL').'/course/detail/'.$course->id.'.html';
            $picUrl = env('IMG_URL').$course->explain_url;
        }

        $data['type'] = WxMessageType::IMAGE;
        $desc = '今日的学习内容，你打卡了吗？';

//        dd($desc);
        $data['list'] = [[
            "title"=>$title,
            "description"=>$desc,
            "url"=>$url,
            "picurl"=>$picUrl
            ]];
        $fans = $this->getPushMsgUsers(WxMessageType::TEACHER,[]);
//        dd($fans);
        foreach($fans as $fan){
            $data['openid'] = $fan->openid;
//            dd($fan->openid);
//            $data['openid'] = "ogGtrv2zRkqvjKu2xbsnOZYRau4I";
//            $data['openid'] = $user->openid;
//            dd($fan);
            event(new WxCustomerMessagePush($data));
        }
        $pushLog = new PushLog();
        $pushLog->course_class_id = $courseId?$courseId:0;
        $pushLog->article_id = $article_id?$article_id:0;
        $pushLog->type = $type;
        $pushLog->description = WxMessageType::trans($type);
        $pushLog->start = '';
        $pushLog->end = '';
        $pushLog->save();

        return $this->getMessage(0,'推送成功');
    }
    /*
     * 获取模版信息
     */
    public function getInfo($type,$author=''){
        switch($type){
            case WxMessageType::COLLECT:
                return '您收藏的系列课程有更新';
            case WxMessageType::UPDATE:
                return '您报名的系列课程有更新';
            case WxMessageType::CREATE:
                return '您关注的导师'.$author->name.'上线新课程了！';
            case WxMessageType::RECOMMEND:
                return '';
        }
    }
    /*
     * 设置直播课
     */
    public function courseLive(Request $request){
        $startTime = $request->input('start');
        $endTime = $request->input('end');
        $id = $request->input('id');
        $isPlay = $request->input('isPlay');
        $playTime = $request->input('playTime');
        $number = $request->input('live_number');
        if(empty($id)){
            return $this->getMessage(1,'未提交课程');
        }
        if($isPlay == 1){
            if(empty($startTime) || empty($endTime)){
                return $this->getMessage(1,'直播时间缺失');
            }
        }
        if(empty($number)){
            return $this->getMessage(1,'未设置频道号');
        }
        $course = Course::where('id',$id)->first();
        $course->is_live = $isPlay;
        $course->live_start_time = $startTime;
        $course->live_end_time = $endTime;
        $course->live_long_time = $playTime;
        $course->live_number = $number;
        if($course->save()){
            return $this->getMessage(0,'设置成功');
        }

        return $this->getMessage(1,'设置失败');
    }

    /*
     * 直播课列表
     */
    public function liveList(Request $request){
        $class_id = $request->input('id');
        $name = $request->input('name');
        if($name){
            $courses = Course::where('course_class_id',$class_id)->where('title','like','%'.$name.'%')->where('is_live',1)->paginate(10);
        }else{
            $courses = Course::where('course_class_id',$class_id)->where('is_live',1)->paginate(5);
        }
        return json_encode(['code'=>0,'body'=>view('course.modal.liveList',['courses'=>$courses,'class_id'=>$class_id,'name'=>$name])->render()]);
    }

    /*
     * 直播设置
     */
    public function liveSet(Request $request){
        $id = $request->input('id');
        $course = Course::where('id',$id)->first();
        $course->is_live = 0;
        if($course->save()){
            return json_encode(['code'=>0,'message'=>'设置成功']);
        }else{
            return json_encode(['code'=>1,'message'=>'设置失败']);
        }
    }
    /*
     * 直播信息详情
     */
    public function liveShow(Request $request){
        $id = $request->input('id');
        $course = Course::where('id',$id)->first();
        return json_encode(['code'=>0,'body'=>view('course.modal.liveShow',['course'=>$course])->render()]);
    }

    public function getPushUsers($type,$courseClass,$author,$start='',$end='',$course_id=''){
        switch($type){
            case WxMessageType::COLLECT:
                $collect = new Collect();
                return $collect->getCollectUser($courseClass->id);
            case WxMessageType::UPDATE:
                $studying = new Studying();
                return $studying->getStudyingUser($courseClass->id);
            case WxMessageType::CREATE:
                return $author->fans($author->id);
            case WxMessageType::RECOMMEND:
                return $author->getRegisterUser($courseClass,$start,$end);
            case WxMessageType::WEIXINNOTICE:
                $studying = new Studying();
                return $studying->getStudyingUser($courseClass->id);
            case WxMessageType::COURSEBUY:
                $studying = new Studying();
                return $studying->getStudyingUser($course_id);
            case WxMessageType::TEACHER:
                $studying = new Studying();
                return $studying->getStudyingUser($course_id);
        }
    }

    /*
     * 获取推送用户
     */
    public function getPushMsgUsers($type, $param = []){
        switch($type){
            case WxMessageType::TEACHER:
                $teacher = new EnrollTeacher();
                return $teacher->getEnrollTeacher();
        }
    }
    /*
     *选择课程
     */
    public function selectCourse(Request $request){
        $title = $request->input('title','');
        $courseClass = new CourseClass();
        if($title){
            $courseClass = $courseClass->where('title','like','%'.$title.'%');
        }
        $courseClass = $courseClass->where('is_live',0)->paginate(10);
        return json_encode(['code'=>0,'body'=>view('course.modal.courseBody',['courseClass'=>$courseClass,'title'=>$title])->render()]);
    }


}
